const Controller = require('egg').Controller;
const fs = require('fs');
const path = require('path');
const sendToWormhole = require('stream-wormhole');
const awaitWriteStream = require('await-stream-ready').write;
const Pump = require('mz-modules/pump');
module.exports = class extends Controller {
    async open_topic_upload() {
        const { ctx } = this;
        // autoFields: true 表示除了文件的其它字段
        let parts = ctx.multipart({ autoFields: true });
        let files = [];
        let field = {}
        let stream;
        let stu_id = '';
        let progress = '';
        // 获取表单中其他数据
        field = parts.field
        // console.log('field:::::::::1',field);这个取不到
        while ((stream = await parts()) != null) {
            console.log('field::::::2', field);
            // 
            stu_id = field.stu_id;
            progress = field.progress;
            if (!stream.filename) {
                // 多文件上传时，只能break,不能return
                break;
            }
            // 定义真实写入的文件名
            const filename = path.basename(stream.filename, path.extname(stream.filename).toLocaleLowerCase()) + Date.now().toString().substring(7) + path.extname(stream.filename).toLocaleLowerCase();
            console.log('filename::::::::', filename);
            let fieldname = stream.fieldname;
            // 写入数据库的文件名
            const fakename = path.basename(stream.filename, path.extname(stream.filename).toLocaleLowerCase()) + Date.now().toString().substring(9);
            const sfile = {
                filename: fakename,
                file_dir: 'openTopic/' + filename,
                progress: field.progress,
                stu_id: field.stu_id,
                canbedelete: 0
            }
            // 对数据库插入上传的文件
            const result1 = await ctx.service.stufile.add(sfile);
            console.log('result1:::::插入成功！', result1);
            // // 目标文件
            const target = path.join('app/public/openTopic', filename);
            let writeStream = fs.createWriteStream(target);
            await Pump(stream, writeStream);
            files.push({
                [fieldname]: target
            });

        }
        if (progress == 4) {
            // 当学生提交通过的论文，我们进入下一阶段
            const result2 = await ctx.service.student.modify({ id: stu_id, progress: 5 });
            ctx.body = {
                files: files,
                field,
                code: 0
            }
            return;
        }
        // 更改学生的进度
        const result2 = await ctx.service.student.modify({ id: stu_id, progress: 2 });
        console.log('result2:::::插入成功！', result2);
        ctx.body = {
            files: files,
            field,
            code: 0
        }
    }
    async mid_term_inspection() {
        const { ctx } = this;
        // autoFields: true 表示除了文件的其它字段
        let parts = ctx.multipart({ autoFields: true });
        let files = [];
        let field = {}
        let stream;
        let stu_id = '';
        let progress = '';
        // 获取表单中其他数据
        field = parts.field
        // console.log('field:::::::::1',field);这个取不到
        while ((stream = await parts()) != null) {
            console.log('field::::::2', field);
            stu_id = field.stu_id;
            progress = field.progress;
            if (!stream.filename) {
                // 多文件上传时，只能break,不能return
                break;
            }
            // 定义真实写入的文件名
            const filename = path.basename(stream.filename, path.extname(stream.filename).toLocaleLowerCase()) + Date.now().toString().substring(7) + path.extname(stream.filename).toLocaleLowerCase();
            console.log('filename::::::::', filename);
            let fieldname = stream.fieldname;
            // 写入数据库的文件名
            const fakename = path.basename(stream.filename, path.extname(stream.filename).toLocaleLowerCase()) + Date.now().toString().substring(9);
            const sfile = {
                filename: fakename,
                file_dir: 'midtermInspection/' + filename,
                progress: field.progress,
                stu_id: field.stu_id,
                canbedelete: 0
            }
            // 对数据库插入上传的文件
            const result1 = await ctx.service.stufile.add(sfile);
            console.log('result1:::::插入成功！', result1);
            // // 目标文件
            const target = path.join('app/public/midtermInspection', filename);
            let writeStream = fs.createWriteStream(target);
            await Pump(stream, writeStream);
            files.push({
                [fieldname]: target
            });

        }
        if (progress == 8) {
            // 当学生提交通过的论文，我们进入下一阶段
            const result2 = await ctx.service.student.modify({ id: stu_id, progress: 9 });
            ctx.body = {
                files: files,
                field,
                code: 0
            }
            return;
        }
        // 更改学生的进度
        const result2 = await ctx.service.student.modify({ id: stu_id, progress: 6 });
        console.log('result2:::::插入成功！', result2);
        ctx.body = {
            files: files,
            field,
            code: 0
        }
    }
    async thesis_inspection() {
        const { ctx } = this;
        // autoFields: true 表示除了文件的其它字段
        let parts = ctx.multipart({ autoFields: true });
        let files = [];
        let field = {}
        let stream;
        let stu_id = '';
        let progress = '';
        // 获取表单中其他数据
        field = parts.field
        // console.log('field:::::::::1',field);这个取不到
        while ((stream = await parts()) != null) {
            console.log('field::::::2', field);
            stu_id = field.stu_id;
            progress = field.progress;
            if (!stream.filename) {
                // 多文件上传时，只能break,不能return
                break;
            }
            // 定义真实写入的文件名
            const filename = path.basename(stream.filename, path.extname(stream.filename).toLocaleLowerCase()) + Date.now().toString().substring(7) + path.extname(stream.filename).toLocaleLowerCase();
            console.log('filename::::::::', filename);
            let fieldname = stream.fieldname;
            // 写入数据库的文件名
            const fakename = path.basename(stream.filename, path.extname(stream.filename).toLocaleLowerCase()) + Date.now().toString().substring(9);
            const sfile = {
                filename: fakename,
                file_dir: 'thesis/' + filename,
                progress: field.progress,
                stu_id: field.stu_id,
                canbedelete: 0
            }
            // 对数据库插入上传的文件
            const result1 = await ctx.service.stufile.add(sfile);
            console.log('result1:::::插入成功！', result1);
            // // 目标文件
            const target = path.join('app/public/thesis', filename);
            let writeStream = fs.createWriteStream(target);
            await Pump(stream, writeStream);
            files.push({
                [fieldname]: target
            });

        }
        // 如果学生是在progress为15的时候进行上传
        if (progress == 15) {
            // 当学生提交通过的论文，我们进入下一阶段
            const result2 = await ctx.service.student.modify({ id: stu_id, progress: 16 });
            ctx.body = {
                files: files,
                field,
                code: 0
            }
            return;
        }
        if (progress == 12) {
            // 当学生提交通过的论文，我们进入下一阶段
            // 如果这个时候reviewed的值为1者不能执行下面这一句，直接跳到16答辩准备阶段
            // 先查询学生的信息
            const stu_msg = await ctx.service.student.findById(stu_id);
            // 这里表示答辩只能执行一次！
            if (stu_msg.reviewed == 1) {
                const result3 = await ctx.service.student.modify({ id: stu_id, progress: 16});
                ctx.body = {
                    files: files,
                    field,
                    code: 0
                }
                return;
            }
            const result2 = await ctx.service.student.modify({ id: stu_id, progress: 13 ,reviewed: 1 });
            ctx.body = {
                files: files,
                field,
                code: 0
            }
            return;
        }
        // 更改学生的进度
        const result2 = await ctx.service.student.modify({ id: stu_id, progress: 10 });
        console.log('result2:::::插入成功！', result2);
        ctx.body = {
            files: files,
            field,
            code: 0
        }
    }
    // 答辩准备用文件上传
    async prepare_reply_inspection() {
        const { ctx } = this;
        // autoFields: true 表示除了文件的其它字段
        let parts = ctx.multipart({ autoFields: true });
        let files = [];
        let field = {}
        let stream;
        let stu_id = '';
        let progress = '';
        // 获取表单中其他数据
        field = parts.field
        // console.log('field:::::::::1',field);这个取不到
        while ((stream = await parts()) != null) {
            console.log('field::::::2', field);
            stu_id = field.stu_id;
            progress = field.progress;
            if (!stream.filename) {
                // 多文件上传时，只能break,不能return
                break;
            }
            // 定义真实写入的文件名
            const filename = path.basename(stream.filename, path.extname(stream.filename).toLocaleLowerCase()) + Date.now().toString().substring(7) + path.extname(stream.filename).toLocaleLowerCase();
            console.log('filename::::::::', filename);
            let fieldname = stream.fieldname;
            // 写入数据库的文件名
            const fakename = path.basename(stream.filename, path.extname(stream.filename).toLocaleLowerCase()) + Date.now().toString().substring(9);
            const sfile = {
                filename: fakename,
                file_dir: 'reply/' + filename,
                progress: field.progress,
                stu_id: field.stu_id,
                canbedelete: 0
            }
            // 对数据库插入上传的文件
            const result1 = await ctx.service.stufile.add(sfile);
            console.log('result1:::::插入成功！', result1);
            // // 目标文件
            const target = path.join('app/public/reply', filename);
            let writeStream = fs.createWriteStream(target);
            await Pump(stream, writeStream);
            files.push({
                [fieldname]: target
            });

        }
        // 当学生的进度为19的时候上传最终版，进入答辩阶段
        if (progress == 19) {
            const result2 = await ctx.service.student.modify({ id: stu_id, progress: 20});
            ctx.body = {
                files: files,
                field,
                code: 0
            }
            return;
        }
        // 更改学生的进度
        const result2 = await ctx.service.student.modify({ id: stu_id, progress: 17 });
        console.log('result2:::::插入成功！', result2);
        ctx.body = {
            files: files,
            field,
            code: 0
        }
    }
    // 审核总结用文件上传
    async audit_summary_inspection() {
        const { ctx } = this;
        // autoFields: true 表示除了文件的其它字段
        let parts = ctx.multipart({ autoFields: true });
        let files = [];
        let field = {}
        let stream;
        let stu_id = '';
        let progress = '';
        // 获取表单中其他数据
        field = parts.field
        // console.log('field:::::::::1',field);这个取不到
        while ((stream = await parts()) != null) {
            console.log('field::::::2', field);
            stu_id = field.stu_id;
            progress = field.progress;
            if (!stream.filename) {
                // 多文件上传时，只能break,不能return
                break;
            }
            // 定义真实写入的文件名
            const filename = path.basename(stream.filename, path.extname(stream.filename).toLocaleLowerCase()) + Date.now().toString().substring(7) + path.extname(stream.filename).toLocaleLowerCase();
            console.log('filename::::::::', filename);
            let fieldname = stream.fieldname;
            // 写入数据库的文件名
            const fakename = path.basename(stream.filename, path.extname(stream.filename).toLocaleLowerCase()) + Date.now().toString().substring(9);
            const sfile = {
                filename: fakename,
                file_dir: 'summary/' + filename,
                progress: field.progress,
                stu_id: field.stu_id,
                canbedelete: 0
            }
            // 对数据库插入上传的文件
            const result1 = await ctx.service.stufile.add(sfile);
            console.log('result1:::::插入成功！', result1);
            // // 目标文件
            const target = path.join('app/public/summary', filename);
            let writeStream = fs.createWriteStream(target);
            await Pump(stream, writeStream);
            files.push({
                [fieldname]: target
            });

        }
        // 当学生的进度为19的时候上传最终版，进入答辩阶段
        if (progress == 24) {
            const result2 = await ctx.service.student.modify({ id: stu_id, progress: 25});
            ctx.body = {
                files: files,
                field,
                code: 0
            }
            return;
        }
        // 更改学生的进度
        const result2 = await ctx.service.student.modify({ id: stu_id, progress: 22});
        console.log('result2:::::插入成功！', result2);
        ctx.body = {
            files: files,
            field,
            code: 0
        }
    }
}
